////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////


package tx.framework.service {

	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import tx.framework.data.social.ISocialUserData;
	import tx.framework.logging.Log;
	import tx.framework.logging.LogEventLevel;
	import tx.framework.utils.ObjectUtil;


	/**
	 *  @author				Dimarik
	 *  @version			1.0
	 *  @langversion		3.0
	 *  @playerversion		9.0
	 *
	 */
	public class VKServiceProxy implements ISocialServiceProxy {

		//---------------------------------------------------------------------
		//
		//   Constructor
		//
		//---------------------------------------------------------------------

		public function VKServiceProxy() {
			super();
			this._vkService = VKService.getInstance();
		}

		//---------------------------------------------------------------------
		//
		//   Private variables
		//
		//---------------------------------------------------------------------

		private var _timer:				Timer = new Timer(0.5e3, 1);

		private var _vkService:			VKService;

		private var _responder:			Responder;

		private var _dataCollection:	Array = new Array(); /* of ISocialUserData */

		//---------------------------------------------------------------------
		//
		//   Interface implementation:  ISocialServiceProxy
		//
		//---------------------------------------------------------------------

		public function getProfile(data:ISocialUserData):void {
			this._dataCollection.push(data);
			if (this._timer.running) return;

			this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.handler_getProfile);
			this._timer.reset();
			this._timer.start();

			//enterFrameBroadcaster.addEventListener(Event.ENTER_FRAME, this.handler_getProfile);
		}

		private function handler_getProfile(event:Event):void {
			(event.currentTarget as IEventDispatcher).removeEventListener(event.type, arguments.callee);
			if (this._dataCollection.length == 0) return;

			if (!this._dataCollection) this._dataCollection = new Array();

			var data:	ISocialUserData;
			var uids:	Array = new Array();

			var len:	uint = this._dataCollection.length;

			for (var i:uint; i < len; i++) {
				data = this._dataCollection[i];
				if (data.suid != '') {
					uids.push(data.suid);
				}
			}

			this._responder = new Responder();
			this._responder.resultCallback = this.handler_getProfilesSuccess;
			this._responder.faultCallback = this.handler_getProfilesFault;

			var fields:String = new Array(
					//VKService.FIELD_PHOTO,
					//VKService.FIELD_PHOTO_MEDIUM,
					VKService.FIELD_PHOTO_BIG
			).join(',');

			this._vkService.getProfiles(this._responder, uids.join(','), fields);

		}

		private function handler_getProfilesSuccess(data:Object):void {
			if (!data) return;

			Log.getLogger('VK-IN-PROXY').log(LogEventLevel.DEBUG, ObjectUtil.toString(data));

			var xml:		XML = new XML(data);
			var userData:	ISocialUserData;
			var suid:		String;
			var len:		uint = this._dataCollection.length;

			var node:		XML;
			var pic:		String;

			// We need to check and assign props to target data
			for (var i:uint = 0; i < this._dataCollection.length;) {
				userData = this._dataCollection[i] as ISocialUserData;
				node = xml.user.(uid == userData.suid)[0];

				if (!node) {
					// keep data in data collection if not yet assigned
					i++;
					continue;
				}

				userData.firstName = node.first_name;
				userData.lastName = node.last_name;
				userData.picture = node.photo_big;
				userData.nick = userData.firstName + ' ' + userData.lastName;
				// remove assigned data
				this._dataCollection.splice(i, 1);
			}

		}

		private function handler_getProfilesFault(data:Object):void {
			this._dataCollection = new Array();
		}

	}
}